package com.lxmes.base.service.impl;

import java.util.List;

import com.lxmes.common.core.domain.entity.SysUser;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.base.mapper.JcIteminfoDistriMapper;
import com.lxmes.base.domain.JcIteminfoDistri;
import com.lxmes.base.service.IJcIteminfoDistriService;

import javax.validation.Validator;

/**
 * 物料储分配送工艺基础表Service业务层处理
 * 
 * @author 张志煜
 * @date 2023-03-21
 */
@Service
public class JcIteminfoDistriServiceImpl implements IJcIteminfoDistriService 
{
    private static final Logger log = LoggerFactory.getLogger(JcIteminfoDistriServiceImpl.class);

    @Autowired
    private JcIteminfoDistriMapper jcIteminfoDistriMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询物料储分配送工艺基础表
     * 
     * @param id 物料储分配送工艺基础表主键
     * @return 物料储分配送工艺基础表
     */
    @Override
    public JcIteminfoDistri selectJcIteminfoDistriById(Long id)
    {
        return jcIteminfoDistriMapper.selectJcIteminfoDistriById(id);
    }

    /**
     * 查询物料储分配送工艺基础表列表
     * 
     * @param jcIteminfoDistri 物料储分配送工艺基础表
     * @return 物料储分配送工艺基础表
     */
    @Override
    public List<JcIteminfoDistri> selectJcIteminfoDistriList(JcIteminfoDistri jcIteminfoDistri)
    {
        return jcIteminfoDistriMapper.selectJcIteminfoDistriList(jcIteminfoDistri);
    }

    @Override
    public String imporIteminfoDistri(List<JcIteminfoDistri> distriList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(distriList) || distriList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (JcIteminfoDistri distri : distriList)
        {
            try
            {
                BeanValidators.validateWithException(validator, distri);
                distri.setCreateBy(operName);
                distri.setItemCode(distri.getItemId());
                distri.setTmh(distri.getItemId());
                distri.setFactoryCode(SecurityUtils.getFactoryCode());
                distri.setFactoryName(SecurityUtils.getFactoryName());
                this.insertJcIteminfoDistri(distri);
                successNum++;
                successMsg.append("<br/>" + successNum + "、物料编号 " + distri.getItemId() + " 导入成功");
              /*  // 验证是否存在这个用户
                SysUser u = userMapper.selectUserByUserName(user.getUserName());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setPassword(SecurityUtils.encryptPassword(password));
                    user.setCreateBy(operName);
                    this.insertUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setUpdateBy(operName);
                    this.updateUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
                }*/
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、物料编号 " + distri.getItemId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 新增物料储分配送工艺基础表
     * 
     * @param jcIteminfoDistri 物料储分配送工艺基础表
     * @return 结果
     */
    @Override
    public int insertJcIteminfoDistri(JcIteminfoDistri jcIteminfoDistri)
    {
        jcIteminfoDistri.setFactoryCode(SecurityUtils.getFactoryCode());
        jcIteminfoDistri.setFactoryName(SecurityUtils.getFactoryName());
        jcIteminfoDistri.setCreateTime(DateUtils.getNowDate());
        return jcIteminfoDistriMapper.insertJcIteminfoDistri(jcIteminfoDistri);
    }

    /**
     * 修改物料储分配送工艺基础表
     * 
     * @param jcIteminfoDistri 物料储分配送工艺基础表
     * @return 结果
     */
    @Override
    public int updateJcIteminfoDistri(JcIteminfoDistri jcIteminfoDistri)
    {
        jcIteminfoDistri.setUpdateTime(DateUtils.getNowDate());
        return jcIteminfoDistriMapper.updateJcIteminfoDistri(jcIteminfoDistri);
    }

    /**
     * 批量删除物料储分配送工艺基础表
     * 
     * @param ids 需要删除的物料储分配送工艺基础表主键
     * @return 结果
     */
    @Override
    public int deleteJcIteminfoDistriByIds(Long[] ids)
    {
        return jcIteminfoDistriMapper.deleteJcIteminfoDistriByIds(ids);
    }

    /**
     * 删除物料储分配送工艺基础表信息
     * 
     * @param id 物料储分配送工艺基础表主键
     * @return 结果
     */
    @Override
    public int deleteJcIteminfoDistriById(Long id)
    {
        return jcIteminfoDistriMapper.deleteJcIteminfoDistriById(id);
    }
}
